import Vue from 'vue'

import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		//公共的变量，这里的变量不能随便修改，只能通过触发mutations的方法才能改变
		userinfo: {},
		menus: {},
		status_height: 44,
		menu_height: 0,
		blackMenu: ['/roadManage/roadSurvey', '/reportForm/accident',
			'/reportForm/technologyMantain',
		], //菜单黑名单需要剔除
	},
	getters: {

	},
	mutations: {
		//相当于同步的操作
		login(state, data) {
			// state.menus = menus
			state.userinfo = data
			uni.setStorageSync('token', data.ACCESS_TOKEN)
			uni.setStorageSync('isLogin', true)
			// uni.setStorageSync("menus", menus)
			uni.setStorageSync("userinfo", data)
			uni.redirectTo({
				url: '/pages/system/system'
			})
		},
		setUserInfo(state, data) {
			state.userinfo = data
		},
		setMenus(state, data) {
			state.menus = data
		},
		logout(state, data) {
			// uni.clearStorageSync()
			uni.removeStorageSync('menus')
			uni.removeStorageSync('token')
			uni.removeStorageSync('userinfo')
			uni.removeStorageSync('authorcode')
			uni.setStorageSync('isLogin', false)
			uni.redirectTo({
				url: "/pages/login/login"
			})
		},
		setStatusHeight(state, data) {
			state.status_height = data
		},
		setMenuHeight(state, data) {
			state.menu_height = data
		}
	},
	actions: {
		//相当于异步的操作,不能直接改变state的值，只能通过触发mutations的方法才能改变
		initState(context) {
			const userinfo = uni.getStorageSync('userinfo') || {
				user_login: ""
			}
			const menus = uni.getStorageSync("menus") || {}

			context.commit('setUserInfo', userinfo)
			context.commit('setMenus', menus)
		}
	}
})
export default store
